#pragma once
#include <iostream>
#include <cstring>
#include <unistd.h>
#include <sys/epoll.h>
//封装epoll函数
class EpollOper
{
public:
    EpollOper() : _flag(126)
    {
        // 创建epfd
        _epfd = epoll_create(_flag);
        if (_epfd < 0)
        {
            std::cout << "epoll_create fail:" << strerror(errno) << std::endl;
            exit(6);
        }
    }
    void EpollCtl(int oper, int sock, uint32_t event)
    {
        epoll_event node;
        node.data.fd = sock;
        node.events = event;
        int ret = epoll_ctl(_epfd, oper, sock, &node);
        if (ret < 0)
        {
            std::cout << "epoll_ctl fail:" << strerror(errno) << std::endl;
            exit(7);
        }
    }
    int EpollWit(epoll_event *event, int maxnum, int timeout)
    {
        int n = epoll_wait(_epfd, event, maxnum, timeout);
        if (n < 0)
        {
            std::cout << "epoll_wait fail:" << strerror(errno) << std::endl;
            exit(8);
        }
        return n;
    }
    ~EpollOper()
    {
        if(_epfd>0) close(_epfd);
    }
private:
    int _epfd;
    const int _flag;
};